package helper;
import java.util.logging.Level;
import java.util.logging.Logger;
import controller.persiapan.*;
import controller.permainan.*;
import java.io.*;
import model.*;
import model.board.*;
import model.players.*;
/**
 * Untuk memmbuat game saved file dan me-loadnya
 */
public class IOSavedGame
{
    //==============================================================================
//MENYIMPAN GAME
    public static void Simpan (String filename, Object G)
    {
       String savedgamefilename = new String ("saved_game/"+filename+".game");

       try
       {
            FileOutputStream fos = new FileOutputStream(savedgamefilename);
            try
            {
                ObjectOutputStream O = new ObjectOutputStream(fos);
                O.writeObject(G);
                O.close();
            }
            catch (IOException ex)
            {
                Logger.getLogger(Permainan2.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        catch (FileNotFoundException ex)
        {
            Logger.getLogger(Permainan2.class.getName()).log(Level.SEVERE, null, ex);
        }
   }

//==============================================================================
    public static void Simpan (String filename, Kotak[] B, Players Q)
    {
       String savedgamefilename = new String ("saved_game/"+filename+".game");

       try
       {
            FileOutputStream fos = new FileOutputStream(savedgamefilename);
            try
            {
                ObjectOutputStream O = new ObjectOutputStream(fos);
                O.writeObject(B);
                O.writeObject(Q);
                O.close();
            }
            catch (IOException ex)
            {
                Logger.getLogger(Permainan2.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        catch (FileNotFoundException ex)
        {
            Logger.getLogger(Permainan2.class.getName()).log(Level.SEVERE, null, ex);
        }
   }

//==============================================================================
//LOAD GAME
    public static Object Load (String filename)
    {
        Object retval = null;
        String savedfile = new String ("saved_game/"+filename);
        try
        {
            FileInputStream fis = new FileInputStream(savedfile); //<---------
            try
            {
                ObjectInputStream I = new ObjectInputStream(fis);//<---------
                try
                {
                    retval = (Game) I.readObject();               //<---------
                }
                catch (ClassNotFoundException ex)
                {
                    Logger.getLogger(Persiapan.class.getName()).log(Level.SEVERE, null, ex);
                }
                I.close();

            }
            catch (IOException ex)
            {
                Logger.getLogger(Persiapan.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        catch (FileNotFoundException ex)
        {
            Logger.getLogger(Persiapan.class.getName()).log(Level.SEVERE, null, ex);
        }

        return retval;
    }
//BACA SAVED GAME FILES YANG ADA DI FOLDER saved_game
    public static String [] getSavedgameList()
    {
        
        File folder = new File("saved_game");
        File[] filelist = folder.listFiles();

        String[] retval = new String[filelist.length];
        for (int i=0; i<filelist.length; i++)
        {
            if (filelist[i].isFile())
            {
                retval[i] = filelist[i].getName();
            }
        }
        
        return retval;
    }

//==============================================================================

    public static void delSavedgame (String delFileName)
    {

        File folder = new File("saved_game");
        File[] filelist = folder.listFiles();

        String[] retval = new String[filelist.length];
        int i = 0;
        while ((i<filelist.length) && (!filelist[i].getName().equals(delFileName)))
        {
            i++;
        }
        if (filelist[i].getName().equals(delFileName))
        {
            filelist[i].delete();
        }
        else
        {

        }
    }

//==============================================================================

    public static boolean isSavedgameExsist (String filename) //gw tau grammarnya salah, tapi biar gampang pake is aja
    {

        File folder = new File("saved_game");
        File[] filelist = folder.listFiles();

        String[] retval = new String[filelist.length];
        int i = 0;
        System.out.println(filelist.length);
        while ((i<filelist.length) && (!filelist[i].getName().equals(filename)))
        {
             System.out.println(i +"  "+ filelist[i].getName());
             i++;
        }
        if (i>=filelist.length)
        {
            return false;
        }
        else if (filelist[i].getName().equals(filename))
        {
            return true;
        }
        else
            return false;
    }

    public static void ListSavedgame () //gw tau grammarnya salah, tapi biar gampang pake is aja
    {

        File folder = new File("saved_game");
        File[] filelist = folder.listFiles();

        String[] retval = new String[filelist.length];

        for (int i = 0; i< filelist.length; i++)
        {
            System.out.println(i +"  "+ filelist[i].getName());
        }

    }


//==============================================================================

    public static void main (String args[])
    {
       IOSavedGame.isSavedgameExsist("ersarwsdfsdf.game");
        IOSavedGame.ListSavedgame();
    }
}
